/**
 * 
 */
package com.jolestar.yayaweibotong.dao;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.jolestar.yayaweibotong.ServiceType;
import com.jolestar.yayaweibotong.YaYaAccessToken;

/**
 * @author jolestar@gmail.com
 * 
 */
public class AccountDomain extends AbstractDomain {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6094943440830324482L;
	public static final String NAME_PROPERTY = "name";
	public static final String SCREEN_NAME_PROPERTY = "screen_name";
	public static final String DOMAIN_PROPERTY = "domain";
	public static final String FOLLOWERS_COUNT_PROPERTY = "followers_count";
	public static final String STATUSES_COUNT_PROPERTY = "statuses_count";
	public static final String FAVOURITES_COUNT_PROPERTY = "favourites_count";
	public static final String FRIENDS_COUNT_PROPERTY = "friends_count";
	public static final String CREATED_AT_PROPERTY = "created_at";
	public static final String BIND_AT_PROPERTY = "bind_at";
	public static final String IS_MASTER_PROPERTY = "is_master";
	public static final String USER_PROPERTY = "user";
	public static final String TOKEN_PROPERTY = "token";
	public static final String TOKEN_SECRET_PROPERTY = "token_secret";
	public static final String LAST_SYNC_STATUS_ID_PROPERTY = "last_sync_status_id";
	public static final String LAST_SYNC_DATE_PROPERTY = "last_sync_date";
	public static final String ACTIVITY_PROPERTY = "activity";
	public static final String PROFILE_IMAGE_PROPERTY = "profile_image";
	public static final String ERROR_PROPERTY = "error";
	public static final String SUSPEND_TO_PROPERTY = "suspend_to";
	public static final String SYNC_FROM_COUNT_PROPERTY = "sync_from_count";
	public static final String SYNC_TO_COUNT_PROPERTY = "sync_to_count";
	public static final String SYNCED_PROPERTY = "synced";

	public AccountDomain(ServiceType serviceType, String uid, UserDomain user,
			YaYaAccessToken token) {
		super(serviceType, uid);
		this.set(USER_PROPERTY, user.getKey());
		this.set(IS_MASTER_PROPERTY, false);
		this.set(TOKEN_PROPERTY, token.getToken());
		this.set(TOKEN_SECRET_PROPERTY, token.getTokenSecret());
	}

	public AccountDomain(Entity entity) {
		super(entity);
	}

	public String getName() {
		return this.get(NAME_PROPERTY);
	}

	public String getScreenName() {
		return this.get(SCREEN_NAME_PROPERTY);
	}

	public void setScreenName(String screenName) {
		this.set(SCREEN_NAME_PROPERTY, screenName);
	}

	public void setDomain(String domain) {
		this.set(DOMAIN_PROPERTY, domain);
	}

	public String getDomain() {
		String domain = this.get(DOMAIN_PROPERTY);
		if(StringUtils.isBlank(domain)){
			domain = this.originalId;
		}
		return domain;
	}
	
	public String getUrl(){
		return this.getServiceType().formatDomainUrl(this.getDomain());
	}

	public void setProfileImage(String img) {
		this.set(PROFILE_IMAGE_PROPERTY, img);
	}

	public String getProfileImage() {
		return this.get(PROFILE_IMAGE_PROPERTY);
	}

	public Date getCreateAt() {
		return this.get(CREATED_AT_PROPERTY);
	}

	public void setCreateAt(Date createAt) {
		this.set(CREATED_AT_PROPERTY, createAt);
	}

	public Date getBindAt() {
		return this.get(BIND_AT_PROPERTY);
	}

	public void setBindAt(Date createAt) {
		this.set(BIND_AT_PROPERTY, createAt);
	}

	public boolean isMaster() {
		return this.get(IS_MASTER_PROPERTY, false);
	}

	public void setMaster(boolean master) {
		this.set(IS_MASTER_PROPERTY, master);
	}

	public boolean isActivity() {
		return this.get(ACTIVITY_PROPERTY, true);
	}

	public void setActivity(boolean activity) {
		this.set(ACTIVITY_PROPERTY, activity);
	}

	public UserDomain getUser() {
		Key userkey = this.get(USER_PROPERTY);
		return DomainFactory.load(userkey);
	}

	public String getLastSyncStatusId() {
		return this.get(LAST_SYNC_STATUS_ID_PROPERTY);
	}

	public void setLastSyncStatusId(String statusId) {
		this.set(LAST_SYNC_STATUS_ID_PROPERTY, statusId);
	}

	public void setLastSyncDate(Date date) {
		this.set(LAST_SYNC_DATE_PROPERTY, date);
	}
	
	public Date getLastSyncDate() {
		return this.get(LAST_SYNC_DATE_PROPERTY);
	}

	public String getToken() {
		return this.get(TOKEN_PROPERTY);
	}

	public String getTokenSecret() {
		return this.get(TOKEN_SECRET_PROPERTY);
	}

	public void setName(String name) {
		this.set(NAME_PROPERTY, name);
	}

	public void setError(String error) {
		this.set(ERROR_PROPERTY, error);
	}

	public String getError() {
		return this.get(ERROR_PROPERTY);
	}

	public boolean isSuspend() {
		long time = this.get(SUSPEND_TO_PROPERTY, 0L);
		if (time <= 0) {
			return false;
		}
		long current = System.currentTimeMillis();
		if (current >= time) {
			return false;
		}
		return true;
	}

	public Date getSuspendToDate() {
		long time = this.get(SUSPEND_TO_PROPERTY, 0L);
		if (time <= 0) {
			return null;
		}
		return new Date(time);
	}

	public void suspendTo(Date date) {
		this.set(SUSPEND_TO_PROPERTY, date.getTime());
	}

	/**
	 * 从该帐号同步过来的微博数目 
	 * @return
	 */
	public long getSyncFromCount() {
		return this.get(SYNC_FROM_COUNT_PROPERTY,0L);
	}

	public long increaseSyncFromCount(int c) {
		long count = this.getSyncFromCount();
		count = count + c;
		this.set(SYNC_FROM_COUNT_PROPERTY, count);
		return count;
	}

	/**
	 * 同步到该帐号的微博数目 
	 * @return
	 */
	public long getSyncToCount() {
		return this.get(SYNC_TO_COUNT_PROPERTY, 0L);
	}

	public long increaseSyncToCount(int c) {
		long count = this.getSyncToCount();
		count = count + c;
		this.set(SYNC_TO_COUNT_PROPERTY, count);
		return count;
	}

	public StatusDomain getLastSyncdStatus() {
		String id = this.getLastSyncStatusId();
		if (id == null) {
			return null;
		}
		return DomainFactory
				.load(StatusDomain.class, this.getServiceType(), id);
	}
	
	/**
	 * 该帐号的微博是否被完全同步
	 * @return
	 */
	public boolean isSynced() {
		return this.get(SYNCED_PROPERTY, true);
	}

	public void setSynced(boolean synced) {
		this.set(SYNCED_PROPERTY, synced);
	}

}
